﻿using TimeSpan.Services;

namespace TimeSpan.Views.Pages;

public partial class RuntimeViewModel(WorkflowService workflow, INavigationService navigation, IMessageService message) : ObservableObject, INavigationAware {

    [ObservableProperty]
    private SpanTask? _task;

    partial void OnTaskChanged(SpanTask? value) {
        if (Task == null) { navigation.Navigate<HomePage>(); }
    }

    [RelayCommand]
    private void Start() {
        Task!.Start();
    }

    [RelayCommand]
    private void Pause() {
        Task!.Pause();
    }

    [RelayCommand]
    private async Task Skip() {
        if (!await message.ConfirmAsync("是否跳过当前阶段")) { return; }
        Task!.Skip();
        if (Task.IsFinish) { await Settle(); }
    }

    [RelayCommand]
    private async Task Cancel() {
        if (!await message.ConfirmAsync("是否取消本次任务")) { return; }
        Task = null;
    }

    [RelayCommand]
    private async Task Settle() {
        if (!await message.ConfirmAsync("是否立即结算本次任务")) { return; }
        Task!.Settle();
        workflow.SettleTask.OnNext(Task);
        Task = null;
    }

    public void OnNavigateTo(object? data) {
        if (data is null) { return; }
        Task = (SpanTask)data;
    }
}